/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import AdminOperations.AdminManageMembers;
import CRUD.ModeratorpermessionsHome;
import Utility.MemberAccountUtility;
import beans.AddModBean;
import beans.AddSuperBean;
import beans.PermToForum;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import xmlMapping.Forum;
import xmlMapping.Member;
import xmlMapping.Moderatorpermessions;

/**
 *
 * @author ABDELMOHSEN
 */
public class AddMod extends org.apache.struts.action.Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        request.getSession().removeAttribute("mesg");
        AddModBean bean=(AddModBean)form;
        MemberAccountUtility util=new MemberAccountUtility();
        Member m=util.getAccountByName(bean.getModName());
        if(m==null){
            request.getSession().setAttribute("mesg", "Username Not found");
            return mapping.findForward("failed");
        }
        if(!m.getType().equals("mem")){
            request.getSession().setAttribute("mesg", "Member already a moderator");
            return mapping.findForward("failed");
        }
        //  Permissions
        Moderatorpermessions permessions=new Moderatorpermessions(m, bean.isEditpost(), bean.isDeletepost(), bean.isEditthread(), bean.isDeletethread(), bean.isEdituser(), bean.isBanuser(),bean.isSuspenduser() , false);
        m.setModeratorpermessions(permessions);
        //ModeratorpermessionsHome home=new ModeratorpermessionsHome();
        //home.attachDirty(permessions);

        //forums
        Vector<PermToForum> allForums=bean.getForums();
        Vector<Forum> permetted=new Vector<Forum>();
        String[] sel=bean.getSelected();
        if(sel==null) {
            request.getSession().setAttribute("mesg", "No Forums Selected");
            return mapping.findForward("failed");
        }
        Forum f=null;
        int x=0;
        for(int i=0;i<sel.length;i++){
            x=Integer.parseInt(sel[i]);
            f=new Forum();
            f.setForumId(x);
            permetted.add(f);
        }
        
        AdminManageMembers model=new AdminManageMembers();
        model.promoteToModerator(m, permetted, (Integer)request.getSession().getAttribute("id"));
        bean.setModName(null);
        return mapping.findForward("done");
    }
}
